package cn.starlightsoftware.sherly.controller.admin.permission;


import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.starlightsoftware.sherly.controller.admin.permission.vo.PermissionDetailReqVO;
import cn.starlightsoftware.sherly.controller.admin.permission.vo.PermissionRespVO;
import cn.starlightsoftware.sherly.controller.admin.permission.vo.PermissionSaveReqVO;
import cn.starlightsoftware.sherly.controller.admin.permission.vo.PermissionSimpleRespVO;
import cn.starlightsoftware.sherly.convertor.PermissionConvertor;
import cn.starlightsoftware.sherly.model.Result;
import cn.starlightsoftware.sherly.db.model.permission.PermissionDO;
import cn.starlightsoftware.sherly.service.permission.PermissionService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.starlightsoftware.sherly.model.Result.success;

/**
 * @author 谷子毅
 */
@Validated
@Tag(name = "admin-权限")
@RestController
@RequestMapping("/system/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @GetMapping("/tree")
    @PreAuthorize("@ss.hasAnyScopes('system:permission:tree')")
    @Operation(summary = "权限树")
    public Result<List<PermissionRespVO>> tree() {
        List<PermissionDO> permissions = permissionService.list();
        return success(PermissionConvertor.buildTree(permissions));
    }
    
    @PostMapping("/save")
    @PreAuthorize("@ss.hasAnyScopes('system:permission:save')")
    @Operation(summary = "权限新增")
    public Result<?> save(@Valid @RequestBody PermissionSaveReqVO reqVO) {
        permissionService.save(reqVO);
        return success();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasAnyScopes('system:permission:update')")
    @Operation(summary = "权限修改")
    public Result<?> update(@Valid @RequestBody PermissionSaveReqVO reqVO) {
        permissionService.update(reqVO);
        return success();
    }

    @GetMapping("/detail")
    @PreAuthorize("@ss.hasAnyScopes('system:permission:detail')")
    @Operation(summary = "权限详情")
    public Result<PermissionRespVO> detail(@ParameterObject PermissionDetailReqVO reqVO) {
        PermissionDO permission = permissionService.detail(reqVO.getPermissionId());
        return success(BeanUtil.copyProperties(permission, PermissionRespVO.class));
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasAnyScopes('system:permission:delete')")
    @Operation(summary = "权限删除")
    public Result<?> delete(@Valid @RequestBody PermissionDetailReqVO reqVO) {
        permissionService.delete(reqVO.getPermissionId());
        return success();
    }

    @GetMapping("simple-tree")
    @Operation(summary = "权限精简树", description = "用于权限选择")
    public Result<List<PermissionSimpleRespVO>> simpleTree() {
        List<PermissionDO> permissions = permissionService.list();
        return success(PermissionConvertor.buildSimpleTree(permissions));
    }
}
